package com.chf.job.service.impl;

import com.chf.job.dao.CompanyMapper;
import com.chf.job.pojo.Company;
import com.chf.job.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    /**
     * 添加更新公司信息
     * @param company
     * @param email
     * @return
     */
    @Override
    public int updateCompany(Company company, String email) {
        // 先去公司表里面查看，有的话就更新，没有的话，就添加
        Company oldCompany = companyMapper.getByEmail(email);
        if (oldCompany == null) {
            Company newCompany = new Company();
            newCompany.setEmail(email);
            newCompany.setCompanyName(company.getCompanyName());
            newCompany.setCity(company.getCity());
            newCompany.setIndustrySector(company.getIndustrySector());
            newCompany.setSeedtime(company.getSeedtime());
            newCompany.setCompanySize(company.getCompanySize());
            newCompany.setUrl(company.getUrl());
            newCompany.setSummary(company.getSummary());
            newCompany.setDetailInrduction(company.getDetailInrduction());
            return companyMapper.insertSelective(newCompany);
        }
        // 跟新公司信息
        Company newCompany = new Company();
        newCompany.setId(oldCompany.getId());
        newCompany.setEmail(email);
        newCompany.setCompanyName(company.getCompanyName());
        newCompany.setCity(company.getCity());
        newCompany.setIndustrySector(company.getIndustrySector());
        newCompany.setSeedtime(company.getSeedtime());
        newCompany.setCompanySize(company.getCompanySize());
        newCompany.setUrl(company.getUrl());
        newCompany.setSummary(company.getSummary());
        newCompany.setDetailInrduction(company.getDetailInrduction());
        return companyMapper.updateByPrimaryKeySelective(newCompany);
    }

    /**
     * 根据邮件查询公司id
     * @param email
     * @return
     */
    @Override
    public Company getByEmail(String email) {

        return companyMapper.getByEmail(email);
    }

    /**
     * 添加公司详细介绍
     * @param company
     * @param email
     * @return
     */
    @Override
    public int addCompanyIntroduce(Company company, String email) {
        return companyMapper.addCompanyIntroduce(company.getDetailInrduction(), email);
    }

    /**
     * 模糊查询所有公司
     * @param positName
     * @return
     */
    @Override
    public List<Company> getByCompanyName(String positName) {
        return companyMapper.getByCompanyName(positName);
    }
}
